package com.seckill.controller;

import com.seckill.common.model.CommonResult;
import com.seckill.common.service.LocalCacheService;
import com.seckill.common.service.RedisService;
import com.seckill.model.dto.ItemDTO;
import com.seckill.model.vo.ItemVo;
import com.seckill.service.ItemService;
import com.seckill.service.PromoService;
import org.joda.time.format.DateTimeFormat;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;
import java.util.List;
import java.util.concurrent.TimeUnit;
import java.util.stream.Collectors;

/**
 * @author lill
 * @date 2019-06-21
 */

@RestController
@RequestMapping("/item")
@CrossOrigin
public class ItemController {
    @Autowired
    private ItemService itemService;

    @Autowired
    private RedisService<ItemDTO> itemDTORedisService;

    @Autowired
    private LocalCacheService<ItemDTO> localCacheService;

    @Autowired
    private PromoService promoService;


    @RequestMapping(value = "/create", method = {RequestMethod.POST})
    public CommonResult createItem(@RequestParam(name = "title") String title,
                                   @RequestParam(name = "price") BigDecimal price,
                                   @RequestParam(name = "stock") Integer stock,
                                   @RequestParam(name = "imgUrl") String imgUrl,
                                   @RequestParam(name = "description") String description) {
        // 封装service请求用来创建商品
        ItemDTO itemDTO = new ItemDTO();
        itemDTO.setTitle(title);
        itemDTO.setPrice(price);
        itemDTO.setStock(stock);
        itemDTO.setImgUrl(imgUrl);
        itemDTO.setDescription(description);

        ItemDTO itemDTOForReturn = itemService.createItem(itemDTO);
        ItemVo itemVO = convertVOFromModel(itemDTOForReturn);

        return CommonResult.success(itemVO);
    }


    @RequestMapping(value = "/get", method = {RequestMethod.GET})
    @ResponseBody
    public CommonResult getItem(@RequestParam(name = "id") Integer id) {

        String key = "item_" + id;
        ItemDTO itemDTO = localCacheService.getLocalCache(key);
        if (null == itemDTO) {
            if (itemDTORedisService.exists(key)) {
                itemDTO = itemDTORedisService.getCache(key);
            } else {
                itemDTO = itemService.getItemById(id);
                itemDTORedisService.setCache(key, itemDTO, 10, TimeUnit.SECONDS);
            }
            localCacheService.setLocalCache(key, itemDTO);
        }
        ItemVo itemVo = convertVOFromModel(itemDTO);
        return CommonResult.success(itemVo);
    }

    @RequestMapping(value = "/list", method = {RequestMethod.GET})
    @ResponseBody
    public CommonResult listItem() {

        List<ItemDTO> itemDTOlList = itemService.listItem();
        List<ItemVo> itemVoList = itemDTOlList.stream().map(itemDTO -> {
            ItemVo itemVo = convertVOFromModel(itemDTO);
            return itemVo;
        }).collect(Collectors.toList());
        return CommonResult.success(itemVoList);
    }


    private ItemVo convertVOFromModel(ItemDTO itemDTO) {
        if (null == itemDTO) {
            return null;
        }
        ItemVo itemVo = new ItemVo();
        BeanUtils.copyProperties(itemDTO, itemVo);
        if (null != itemDTO.getPromoDTO()) {
            //有正在进行或即将进行的秒杀活动
            itemVo.setPromoStatus(itemDTO.getPromoDTO().getStatus());
            itemVo.setPromoId(itemDTO.getPromoDTO().getId());
            itemVo.setStartDate(itemDTO.getPromoDTO().getStartDate().toString(DateTimeFormat.forPattern("yyyy-MM-dd HH:mm:ss")));
            itemVo.setEndDate(itemDTO.getPromoDTO().getEndDate().toString(DateTimeFormat.forPattern("yyyy-MM-dd HH:mm:ss")));
            itemVo.setPromoPrice(itemDTO.getPromoDTO().getPromoItemPrice());
        } else {
            itemVo.setPromoStatus(1);
        }
        return itemVo;
    }


    @RequestMapping(value = "/publishPromo", method = {RequestMethod.GET})
    @ResponseBody
    public CommonResult publishPromo(@RequestParam(name = "id") Integer id) {
        promoService.publishPromo(id);
        return CommonResult.success();

    }
}
